<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 文件缓存驱动
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Cache_File extends EGP_Cache_Abstract
{

    /**
     * 缓存配置相关参数
     *
     * @var array
     */
    protected $_options = array(
       'lifeTime' => 259200, //缓存生存周期(秒)
       'automaticSerialization' => true, //是否开启自动序列化
       'cacheDir' => '/.cache', //缓存文件保存目录
    );

    /**
     * 用于内部存储数据
     *
     * @var array
     */
    protected $_data;

    /**
     * 设置缓存存取参数信息
     *
     * @param  array  $options
     */
    public function setOptions(array $options)
    {
        parent::setOptions($options);
        $this->_options['cacheDir'] = EGP_Io::strip($this->_options['cacheDir']);
        EGP_Io::mkdir($this->_options['cacheDir']);
    }

    /**
     * 保存缓存资料
     *
     * @param  mixed   $value
     * @param  string  $id
     * @return boolean
     * @throws EGP_Exception
     */
    public function save($data, $id)
    {
        $saveData = array('savetime' => time(), 'data' => $data);
        $this->_data[$id] = $data;

        if ($this->_options['automaticSerialization']) {
            $saveData = serialize($saveData);
        }

        if (!is_dir($this->_options['cacheDir'])) {
            throw new EGP_Exception('指定的缓存目录不存在：' . $this->_options['cacheDir']);
        }

        return file_put_contents($this->_getFileName($id), $saveData, LOCK_EX);
    }

    /**
     * 加载缓存数据
     *
     * @param  string   $id
     * @param  integer  $lifeTime
     * @return mixed|false
     */
    public function load($id, $lifeTime = null)
    {
        if (isset($this->_data[$id])) {
            return $this->_data[$id];
        }

        $cacheFile = $this->_getFileName($id);
        if (is_readable($cacheFile) && $saveData = @file_get_contents($cacheFile)) {

            if ($this->_options['automaticSerialization']) {
                $saveData = unserialize($saveData);
            }

            if ($this->_options['lifeTime'] != 0
                && (time() - filemtime($cacheFile)) >= $this->_options['lifeTime']) {
                $this->remove($id);
            }

            if (!is_null($lifeTime) && $saveData['savetime'] + $lifeTime < time()) {
                $this->remove($id);
            }
            $this->_data[$id] = $saveData['data'];

            return $this->_data[$id];
        }
        return false;
    }

    /**
     * 移除缓存数据
     *
     * @param  string  $id
     * @return boolean
     */
    public function remove($id)
    {
        unset($this->_data[$id]);
        return @unlink($this->_getFileName($id));
    }

    /**
     * 移除所有缓存数据
     *
     * @return boolean
     */
    public function clean()
    {
        $files = scandir($this->_options['cacheDir']);
        foreach ($files as $file) {
            $file = $this->_options['cacheDir'] . DIR_SEP . $file;
            if (is_file($file)) {
                @unlink($file);
            }
        }
        return true;
    }

    /**
     * 获取缓存文件名
     *
     * @param  string  $id
     * @return string
     */
    protected function _getFileName($id)
    {
        return $this->_options['cacheDir'] . DIR_SEP . md5($id);
    }

    /**
     * 获取缓存状态
     *
     * @return array
     */
    public function getStats()
    {
        return stat($this->_options['cacheDir']);
    }
}